// pages/W_ProductDetail/index.js
var CommentUtils = getApp().CommonUtils;
Page({
  /**
   * 页面的初始数据
   */
	data: {
		P_ID: '',
		Is_SHARE:'0',
		StarImage: {
			StarImageIndex: ['0.5', '1.0', '1.5', '2.0', '2.5', '3.0', '3.5', '4.0', '4.5', '5.0'],
			StarImagePath: ['../../images/w/26.png', '../../images/w/27.png', '../../images/w/28.png', '../../images/w/29.png', '../../images/w/30.png', '../../images/w/31.png', '../../images/w/32.png', '../../images/w/33.png', '../../images/w/34.png', '../../images/w/35.png'],
		},

		Tap_Text: ['产品详情', '评价'],//商品详情或评价文字
		Tap_SelectIndex: 0, //商品详情或评价的选择
		BuyNum: 1,//购买数量
		IsCollection: false,//商品是否被收藏：默认false未被收藏
		carNum: "",
	},

  /**
   * 生命周期函数--监听页面加载
   */
	onLoad: function (options) {
		console.log('options', options)

		this.setData({
			P_ID: options.pid,
			Is_SHARE: options.share
		})
		console.log('Is_SHARE', options.share)
		if (getApp().data.Token == '') {
			this.requestNetGetJSPIOpenID('');
		} else {
			this.requestNetProductDetial(this.data.P_ID);
			this.requestNetProductImageDetial(this.data.P_ID);
			this.requestNetProductCommentList(this.data.P_ID);
		}
	},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
	onReady: function () {

	},

  /**
   * 生命周期函数--监听页面显示
   */
	onShow: function () {
		if (getApp().data.Token != '') {
			this.getcarNum();
		}
	},



  /**
   * 生命周期函数--监听页面隐藏
   */
	onHide: function () {

	},

  /**
   * 生命周期函数--监听页面卸载
   */
	onUnload: function () {

	},

	/**
 * 用户点击右上角分享
 */
	onShareAppMessage: function () {
		var _this = this;
		return {
			title: _this.data.ProductDetial.P_Name,
			path: '/pages/W_ProductDetail/index?pid=' + _this.data.P_ID +'&share=1',
		}
	},

	/**
	 * 网络请求--用户登录并获取Token
	 */
	requestNetGetJSPIOpenID: function (scene) {
		var _this = this;
		wx.login({
			success: res => {
				if (res.code) {
					wx.request({
						url: 'https://www.chnode.xyz/api/Other.ashx',
						data: {
							'DataType': 'GetJSPIOpenID',
							'js_code': res.code,
							'FUserID': scene
						},
						success: function (e) {
							if (e.data.isok == 'true') {
								getApp().data.openid = e.data.data.openid
								getApp().data.Token = e.data.data.Token
								_this.requestNetProductDetial(_this.data.P_ID);
								_this.requestNetProductImageDetial(_this.data.P_ID);
								_this.requestNetProductCommentList(_this.data.P_ID);
								_this.getcarNum();
							} else {
								wx.showModal({
									title: '提示',
									content: e.data.info,
									showCancel: false,
								})
							}
						},
						fail: function (e) {
						}
					})
				}
			}
		})
	},

	getcarNum: function () {
		var _this = this;
		wx.request({
			url: "https://www.chnode.xyz/api/Car.ashx?",
			data: {
				DataType: "CarCount",
				Token: getApp().data.Token,
				AS_ID: getApp().data.AS_ID,
				A_ID: getApp().data.A_ID,
			},
			success: function (res) {
				if (res.data.isok == "false") {

				} else {
					_this.setData({
						carNum: res.data.data.Number,
					})
				}
			}
		})
	},

	/**
	 * 网络请求--商品详情
	 */
	requestNetProductDetial: function (pid) {
		var _this = this;
		wx.showNavigationBarLoading();
		wx.request({
			url: 'https://www.chnode.xyz/api/Product.ashx?DataType=Product_Detial',
			data: {
				'Token': getApp().data.Token,
				'pid': pid,
				AS_ID: getApp().data.AS_ID,
				A_ID: getApp().data.A_ID,
			},
			success: function (res) {
				if (res.data.isok == "true") {
					_this.setData({
						IsCollection: res.data.datas[0].issc != '0',
						ProductDetial: res.data.datas[0]
					});
				}
			},
			complete: function () {
				wx.hideNavigationBarLoading()
			}
		})
	},

	/**
 	* 网络请求--商品详情图
 	*/
	requestNetProductImageDetial: function (pid) {
		var _this = this;
		wx.showNavigationBarLoading();
		wx.request({
			url: 'https://www.chnode.xyz/api/Product.ashx?DataType=Product_Image_Detial&pid=' + pid,
			success: function (res) {
				if (res.data.isok == "true") {
					_this.setData({
						ProductImageDetial: res.data.datas
					});
				}
			},
			complete: function () {
				wx.hideNavigationBarLoading()
			}
		})
	},

	/**
		* 网络请求--商品评价列表
		*/
	requestNetProductCommentList: function (pid) {
		var _this = this;
		wx.showNavigationBarLoading();
		wx.request({
			url: 'https://www.chnode.xyz/api/Product.ashx?DataType=Product_Comment_List&pid=' + pid,
			success: function (res) {
				if (res.data.isok == "true") {
					var datas = res.data.datas;
					if (datas != null && datas.length > 0) {
						for (let i = 0, j = datas.length; i < j; i++) {
							var index = _this.data.StarImage.StarImageIndex.indexOf(datas[i].C_Score)
							var star_image_path = _this.data.StarImage.StarImagePath[index];
							datas[i].StarImage = star_image_path;
						}
						_this.setData({
							ProductCommentList: datas
						});
					}
				}
			},
			complete: function () {
				wx.hideNavigationBarLoading()
			}
		})
	},

	/**
 * 点击事件--选择商品详情或评论
 */
	btnEventTapSelect: function (res) {
		this.setData({
			Tap_SelectIndex: res.currentTarget.dataset.index,
		})
	},

	/**
	 * 点击事件--购买数量-1
	 */
	btnEventNumDel: function () {
		if (this.data.BuyNum >= 2) {
			this.setData({
				BuyNum: --this.data.BuyNum,
			})
		}
	},

	/**
	 * 点击事件--购买数量+1
	 */
	btnEventNumAdd: function () {
		this.setData({
			BuyNum: ++this.data.BuyNum,
		})
	},

	/**
	 * 点击事件--评论查看大图
	 */
	btnEventEvaluationImage: function (e) {
		var currentImg = e.currentTarget.dataset.image;
		var images = e.currentTarget.dataset.images;
		var currentImgs = [];
		for (let i = 0, j = images.length; i < j; i++) {
			currentImgs[i] = images[i].CI_Image;
		}
		wx.previewImage({
			current: currentImg, // 当前显示图片的http链接
			urls: currentImgs // 需要预览的图片http链接列表
		})
	},

	/**
	 * 点击事件--客服
	 */
	btnEventService: function () {

		wx.navigateTo({
			url: '../W_CustomCenter/index',
		})
	},

	/**
	 * 点击事件--添加或取消收藏
	 */
	btnEventCollention: function () {
		var _this = this;
		var Collection_Type = _this.data.IsCollection ? 'P_CollectionCancel' : 'CollectionAdd';
		wx.showNavigationBarLoading();
		wx.request({
			url: 'https://www.chnode.xyz/api/Collection.ashx?DataType=' + Collection_Type,
			data: {
				P_ID: _this.data.P_ID,
				Token: getApp().data.Token
			},
			success: function (res) {
				if (res.data.isok == "true") {
					CommentUtils.showToastWithImg(res.data.info, 1)
					_this.setData({
						IsCollection: !_this.data.IsCollection,
					})
				} else {
					CommentUtils.showToastWithImg(res.data.info, 2)
				}
			},
			fail: function () {
				CommentUtils.showToastWithImg("稍后尝试", 3)
			},
			complete: function () {
				wx.hideNavigationBarLoading()
			}
		})
	},

	/**
	 * 点击事件--跳转购物车
	 */
	btnEventJumpCar: function (e) {
		wx.switchTab({
			url: '../Fl_Shop-cart/index',
		})
	},

	/**
	 * 点击事件--加入购物车
	 */
	btnEventAddCar: function () {
		var _this = this;
		wx.request({
			url: 'https://www.chnode.xyz/api/Car.ashx?DataType=CarAdd',
			data: {
				Token: getApp().data.Token,
				P_ID: _this.data.P_ID,
				Number: _this.data.BuyNum,
				AS_ID: getApp().data.AS_ID,
				A_ID: getApp().data.A_ID,
			},
			success: function (res) {
				if (res.data.isok == "true") {
					CommentUtils.showToastWithImg("添加购物车成功", 1)
					_this.getcarNum();
				} else {
					CommentUtils.showToastWithImg("添加购物车失败", 2)
				}
			},
			fail: function () {
				CommentUtils.showToastWithImg("稍后尝试", 3)
			},
			complete: function () {
				wx.hideNavigationBarLoading()
			}
		})
	},

	btnBack:function(){
		wx.switchTab({
			url: '../Fl_Index2/index'
		})
	}
})
